﻿Imports Microsoft.Office.Interop
Imports System.Collections.Generic

Public Class PowerUsageReader
    Private WithEvents moRS232 As New Rs232
    Private Delegate Sub CommEventUpdate(ByVal source As Rs232, ByVal mask As Rs232.EventMasks)
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            With moRS232
                If .IsOpen Then
                    .Close()
                    System.Threading.Thread.Sleep(500)
                End If
                .Port = Integer.Parse(ComboBox1.Text)
                .BaudRate = 9600
                .DataBit = 8
                .StopBit = Rs232.DataStopBit.StopBit_1
                .Parity = Rs232.DataParity.Parity_None
                .Timeout = 1500
                .Open()
                .Dtr = False
                .Rts = False
                .EnableEvents() 'data is received using event
                .Write("1") 'write something to COM port to initiate data transfer
                'com port is left open because you never know when the data transfer has ended
            End With
        Catch Ex As Exception
            MessageBox.Show(Ex.Message, "Connection Error", MessageBoxButtons.OK)
        End Try
    End Sub

    Private Sub RS232_CommEvent(ByVal source As Rs232, ByVal Mask As Rs232.EventMasks) Handles moRS232.CommEvent
        If (Mask And Rs232.EventMasks.RxChar) > 0 Then
            TextBox1.AppendText(source.InputStreamString)
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Dim FlashCountList As New List(Of Integer)
            For Each Line As String In TextBox1.Text.Split(CChar(vbCrLf))
                If Line <> "" And Line.Contains(vbTab) Then
                    Dim LineParts As New List(Of String)
                    LineParts.AddRange(Line.Split(CChar(vbTab)))
                    FlashCountList.Add(Integer.Parse(LineParts(1)))
                End If
            Next


            Dim ExcelApp As Excel.Application
            Dim ExcelWBook As Excel.Workbook
            Dim ExcelWSheet As Excel.Worksheet
            ExcelApp = New Excel.Application
            ExcelWBook = ExcelApp.Workbooks.Add
            ExcelWSheet = DirectCast(ExcelWBook.Sheets.Item(1), Excel.Worksheet)
            ExcelApp.Visible = True

            Dim ItemDateTime As Date = Date.Parse(DateTimePicker1.Value.Date & " " & MaskedTextBox1.Text)
            CType(ExcelWSheet.Columns(1), Excel.Range).NumberFormat = "dd/MMM/yyyy hh:mm"
            CType(ExcelWSheet.Columns(2), Excel.Range).NumberFormat = "dddd hh:mm"
            CType(ExcelWSheet.Cells(1, "C"), Excel.Range).Value2 = "kWh"
            Dim RowCounter As Integer = 2
            For Each Item As Integer In FlashCountList
                CType(ExcelWSheet.Cells(RowCounter, "C"), Excel.Range).Value2 = Item / 10000
                CType(ExcelWSheet.Cells(RowCounter, "A"), Excel.Range).Value2 = ItemDateTime
                CType(ExcelWSheet.Cells(RowCounter, "B"), Excel.Range).Value2 = ItemDateTime
                ItemDateTime = ItemDateTime.AddHours(1)
                RowCounter += 1
            Next
            ExcelWSheet.Columns.AutoFit()
        Catch
        End Try
    End Sub

    Private Sub PowerUsageReader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")

        If My.Settings.StartDate = DateTime.MinValue Then
            DateTimePicker1.Value = Now.Date
        Else
            DateTimePicker1.Value = My.Settings.StartDate
        End If

        If My.Settings.StartTime = DateTime.MinValue Then
            MaskedTextBox1.Text = Now.TimeOfDay.ToString
        Else
            MaskedTextBox1.Text = My.Settings.StartTime.TimeOfDay.ToString
        End If

        TextBox1.Text = My.Settings.Output
        ComboBox1.Text = My.Settings.COMport
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        TextBox1.Clear()
    End Sub

    Private Sub PowerUsageReader_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        My.Settings.StartDate = DateTimePicker1.Value
        My.Settings.StartTime = Date.Parse(MaskedTextBox1.Text)
        My.Settings.Output = TextBox1.Text
        My.Settings.COMport = ComboBox1.Text
        My.Settings.Save()
    End Sub
End Class